package com.heima.bos.service;

import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.heima.bos.dao.RoleDaoInter;
import com.heima.bos.dao.impl.BaseDaoImpl;
import com.heima.bos.dao.impl.UserDao;
import com.heima.bos.domain.Role;
import com.heima.bos.domain.User;
@Service
@Transactional
public class UserService extends BaseService<User> {
	@Resource
	private UserDao userDao;
	@Resource 
	private RoleDaoInter roleDao;
	@Resource
	private ProcessEngine processEngine;
	@Override
	public BaseDaoImpl<User> getDao() {
		return userDao;
	}
	//添加用户
	public void save(User entity, String[] roleIds) {
		userDao.save(entity);
		//用户同步
		org.activiti.engine.identity.User actUser = new UserEntity();
		actUser.setId(entity.getId());
		processEngine.getIdentityService().saveUser(actUser);
		Set roles = entity.getRoles();
		Role role = null;
		for (String id : roleIds) {
			role = roleDao.findById(id);
			//同步
			processEngine.getIdentityService().createMembership(actUser.getId(), role.getName());
			roles.add(role);
		}
	}
}
